#ifndef AUDIOCAPTUREWIDGET_H
#define AUDIOCAPTUREWIDGET_H


// Qt
#include <QApplication>
#include <QRect>
#include <QDesktopWidget>
#include <QtCore/QTimer>
#include <QtMultimedia/QAudio>
#include <QtMultimedia/QAudioInput>
#include <QtMultimedia/QAudioOutput>
#include <QtMultimedia/QAudioFormat>
#include <QtMultimedia/QAudioDeviceInfo>
#include <QtMultimedia/QAudioDeviceInfo>
#include <QFile>
#include <QBuffer>
#include <QtEndian>
#include <QGridLayout>
#include <QPalette>
#include <QMargins>
#include <QtGlobal>
#include <QVector>

#include <QtDebug>

// qwt plot
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_series_data.h>
#include <qwt_plot_marker.h>
#include <qwt_symbol.h>
#include <qwt_dial.h>

// fftw
#include <fftw3.h>

// misc
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <utilities.h>
#include <vector>






class AudioCaptureWidget:public QWidget {
        Q_OBJECT

public:
        AudioCaptureWidget(QWidget *parent = 0);
        ~AudioCaptureWidget();

        // function to start and stop the trial
        void start();
        void stop();

        // sampling frequency for audio recording
        int freq;   // frequency of sampling

        // loop time. notifies fft function to perform fft when audio data is ready
        int notifyInterval;

        // finds the peak and the frequency at which the peak occurs
        double maxAmp;
        int maxInd;

        // counts number of loops executed
        int loopCount;

        // plot widget
        QwtPlot *fftPlot;
        QwtPlot *audioSignalPlot;

        // curve to plot fft
        QwtPlotCurve *fftCurve;
        QwtPlotCurve *audioSignalCurve;
        QwtPlotCurve *thresholdLine;

       // marker to indicate peak
        QwtPlotMarker marker;

        // symbol for marker
        QwtSymbol symb;

        // FFT variables
        //fftw_real in[8000], out[8000], spectrum[8000/2+1];
        //double in[8000];
        //double spectrum[8000];
        //fftw_complex out[8000];

        int complete;

        // stores threshold value. if fft peak is greater than threshold then trial stops
        long HLThreshold;
        long LLThreshold;
        int stepCountLimit;
        int actualStepCount;
        int peakFreqLimit;
        int peakFreqLimitInd;
        int band;


        // threshold line points
        double xLine[2];
        double yLine[2];


        // timer that controls audio recording and fft analysis
        QTimer *audioTimer;

         // QAudio widget object
         QAudioInput *audio;

         QBuffer *audioBuffer; /**< audio buffer */
         int totalSize; /**< total buffer size */
         int len; /**< length of sample */
         int *rawAudio; /**< data array to store raw audio data */
         double *rawAudioD; /**< converting rawAudio to double */
         double *xAxis; /**< x-axis ticks*/
         QwtSymbol *peakSymbol; /**< Peak amplitude in fft */
         double *peakFrequencyArray; /**< Keep track of peak frequency*/
          double *loopCountArray; /**< Keep track of loopCount */

        int *noise;
        int noiseSamples;
        int noiseCount;


         double dataOld[2000];
         double spectrumOld[2000];
         double dataDiff[2000];


public slots:
        // is called when audio data is ready for analysis.
        void dataReady();
        void statePrint(QAudio::State state);
        void bytesWritten(qint64);

signals:
        // indicates when fft analysis is done
       void fftDone();

       // indicates when trial is complete
       void trialComplete();




private:


        // not used
        QAudioOutput *audioOut;

        // stores format of audio to be recorded
        QAudioFormat format;

        // not used
        QIODevice *input;
};

#endif // AUDIOCAPTUREWIDGET_H
